<?php

//***********************************************参数设置
global $max_file_size,$uptypes,$upFormats,$dir,$imgpreview,$sizeformat,$newname,$width,$height;
$max_file_size=104857600;//这里以字节为单位(1024*2)*1024=2097152 = 2M
$uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png','application/msword','application/vnd.ms-excel','application/rtf','application/pdf','application/zip','application/mspowerpoint','audio/mpeg','application/x-shockwave-flash','audio/x-wav','video/mpeg','video/vnd.rn-realvideo','application/x-visio','audio/mp3','video/mpeg4','image/tiff','text/plain','audio/x-ms-wma','video/x-ms-wmv','application/octet-stream','application/x-zip-compressed','application/x-dosexec');//上传类型
$upFormats=array('jpg', 'jpeg','gif','bmp','png','xls','doc','vsd','dot','ppt','txt','rtf','pdf','zip','ppt','pps','mp3','wma','wmv','mp4','rm','tiff','wmv','exe','rar','dll');//上传格式后缀
$dir="uploadfiles/";                  //上传文件路径
$imgpreview=2;                        //是否生成预览图(1为生成,其他为不生成);
$newname="sw_";                     //缩微图片前缀名称
$width="100";                    //缩微宽度
$height="100";                   //缩微高度
$sizeformat = 'k'; //最后显示大小

//上传类
class upload{
//************************************************检查上传文件是否是post上传及上传是否为空值
function uppost(){
            if(!is_uploaded_file($_FILES["upfile"]["tmp_name"]))
			  {
			    $msg="文件上传错误，请联系林聪。代码:up_007。";
		        echo $this -> alert($msg);
				//echo "<script language=javascript> parent.document.getElementById('msg').innerHTML = '2222222';<//script>";
		        exit;
              }
}

//************************************************判断上传类型
function uptype(){
        //检查文件类型
       global $uptypes,$upFormats,$f_name;
		if(!in_array($_FILES["upfile"]["type"] , $uptypes))
		{
		$msg="上传文件格式受限，请联系林聪，代码:up_001。";
		echo $this -> alert($msg);
		exit;
		}
        else{
	      //mema类型来判断不是很安全，所以在次判断类型，生成对应后缀。
		  /*
		  switch ($_FILES["upfile"]["type"])///检查上传的类型
		  {     
		  case   "image/pjpeg":   //IE格式下的 
		  $fileextname   =   "jpg";     
		  break;
		  case   "image/jpeg"    //firefox格式下的
		  $fileextname   =   "jpg";     
		  break;
		  case   "image/gif":     
		  $fileextname   =   "gif";     
		  break;     
		  case   "image/x-png":     
		  $fileextname   =   "png";     
		  break;     
		  case   "application/x-shockwave-flash":     
		  $fileextname   =   "swf";     
		  break;     
		  case   "text/plain":     
		  $fileextname   =   "txt";     
		  break;     
		  case   "application/msword":     
		  $fileextname   =   "doc";     
		  break;     
		  case   "application/x-zip-compressed":     
		  $fileextname   =   "zip";     
		  break; 
		  case   "audio/mpeg":
		  $fileextname   =   "mp3";     
		  break; 
		  case   "audio/x-ms-wma":
		  $fileextname   =   "wma";     
		  break; 
		  default://如果取不到类型,那么取文件名后3个数值并转小写
		  $fileextname  =strtolower(substr(strrchr(trim($_FILES['upfile']['name']), "."),1,4));
		  }
		  
		  */
          

		 //判断后缀,更加安全
		 $f_name=$_FILES['upfile']['name']; 
		 $exten=explode('.',strtolower($f_name)); 
		 $sz=sizeof($exten)-1; 
		 $flag=false;
		 foreach($upFormats as $item) 
		      { 
			    if($exten[$sz]==$item) 
				{ 
				  $flag=true;
				} 
			   }
			   if($flag)
			   {
                $fileextname=$exten[$sz];
			   }
			   else
			   {
               $msg="上传文件格式后缀受限,请联系林聪。代码:up_002";
		       echo $this -> alert($msg);
			   exit();
			   } 
			  
		}
        return $fileextname;
}
//***********************************************************判断文件大小
function upsize(){
      global $max_file_size,$sizeformat;
	  if($_FILES["upfile"]["size"]==0){
		$msg="请选择上传得文件";
		echo $this -> alert($msg);
	    exit();
	  }
      else{
           if($_FILES["upfile"]["size"]>$max_file_size){
             $msg="文件超过了最大限制！请联系林聪。代码:up_003";
		     echo $this -> alert($msg);
	         exit();
            }
            else{
			   switch (strtolower($sizeformat)){
			   case 'b':
			   return $_FILES["upfile"]["size"] .'B';
			   break;
			   case 'k':
			   return ($_FILES["upfile"]["size"]/1024) .'K';
			   break;
			   case 'm':
			   return ($_FILES["upfile"]["size"])/(1024*1024) .'M';}
			   }
	        }
}
//***************************************************************判断文件夹是否存在,没有存在则创建,每月生成以个文件夹
 function upfolder(){
	global $dir;
    if(!file_exists($dir)){
        mkdir($dir,0777);
     }
     $foldername=idate("Y").idate("m");
     if(!file_exists($dir."/".$foldername)){
        mkdir($dir."/".$foldername,0777);
     }
	 $dir_folder=$dir.$foldername."/";
	 return $dir_folder;
 }
//***************************************************************判断文件是否有写得权限	
function writefolder($dir_folder){
   global $dir;
   if(!is_writable($dir_folder)||!is_writable($dir)){
        $msg="传目录没有写得权利！请联系林聪。代码:up_004";
		echo $this -> alert($msg);
	    exit();
     }
 }
//*****************************************************************生成随机文件名
function upname($dir_folder,$fileextname){

    $filename = time().".".$fileextname;
	if (file_exists($dir_folder.$filename)) 
    {
		$msg="文件名重名了！请联系林聪。代码:up_005";
		echo $this -> alert($msg);
	    exit();
	}
	
	/*else{
		echo $filename;
    }*/
    return $filename;
}
//********************************************************************生成缩微图片,注明目前只加了三种图片缩微
function thumb($cat,$oldname,$newname,$width,$height){
  $srcFile = $cat.$oldname;
  $data = getimagesize($srcFile);
  $dscFile = $cat. $newname;

   switch ($data[2]) {
    case 1:
    $im = imagecreatefromgif($srcFile);
    break;

    case 2:
    $im = imagecreatefromjpeg($srcFile);
    break;

    case 3:
    $im = imagecreatefrompng($srcFile);
    break;
   }

   $srcW=imagesx($im);
   $srcH=imagesy($im);

   if(($srcW/$width)>=($srcH/$height)){
    $temp_height=$height;
    $temp_width=$srcW/($srcH/$height);
    $src_X=abs(($width-$temp_width)/2);
    $src_Y=0;
   }
   else{
    $temp_width=$width;
    $temp_height=$srcH/($srcW/$width);
    $src_X=0;
    $src_Y=abs(($height-$temp_height)/2);
   }

   $temp_img=imagecreatetruecolor($temp_width,$temp_height);
   imagecopyresized($temp_img,$im,0,0,0,0,$temp_width,$temp_height,$srcW,$srcH);
   $ni=imagecreatetruecolor($width,$height);
   imagecopyresized($ni,$temp_img,0,0,$src_X,$src_Y,$width,$height,$width,$height);
   $cr = imagejpeg($ni,$dscFile);
   if ($cr){
    chmod($dscFile, 0777);
    return true;
   }
}


//********************************************************************移动文件即上传,两个函数都可以
function up($dir_folder,$filename){
        if(!move_uploaded_file ($_FILES["upfile"]["tmp_name"],$dir_folder.$filename)){  
            $msg="移动文件出错，请联系林聪。代码:up_006";
		    echo $this -> alert($msg);
	        exit();
           }
		   else{
			 $msg="文档上传成功，恭喜！";
		     echo $this -> alert($msg);
           }
     /*
	   if(copy($dir_folder,$filename)){
		 echo "移动文件出错!";
		 exit();
	   }
	   else{
		 echo "上传成功";
       }
      */
 
      }
//***********************************************************************错误显示
function alert($msg)
{
    $erro= "<script language=javascript> parent.document.getElementById('up_err_msg').innerHTML = '提示信息：".$msg."';</script>";
    return $erro; 
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上传</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
}
-->
</style></head>
<body style="font-size:12px;">
<form action="?action=init" method="post" enctype="multipart/form-data" name="upform">
<input name="upfile" id="upfile" type="file"  value=""  style="width:200px; border:1px solid #9a9999; font-size:9pt; background-color:#ffffff" size="17" >
<input type="submit" value="上传" style="width:30px; border:1px solid #9a9999; font-size:9pt; background-color:#ffffff" size="17">
<input name="scan" type="hidden" id="up" value="true">
<br>
</form> 
</body>
</html> 

<?php
if(isset($_GET['action'])){
	if($_GET['action']=="init"){
//声明一个上传类

$upload = new upload(); 
$upload -> uppost();
$filesize=$upload -> upsize();
$fileextname=$upload -> uptype();
$dir_folder=$upload -> upfolder();
$upload -> writefolder($dir_folder);
$filename=$upload -> upname($dir_folder,$fileextname);
//$old_file_name=upname($dir_folder,$fileextname);
$upload -> up($dir_folder,$filename);
$file_sha1=sha1_file($dir_folder.$filename);
//打开文件
$file_open=fopen($dir_folder.$filename,"r");
//$fow=fileowner($dir_folder.$filename);
$fw = fstat($file_open);
//$fw1 = stat($file_open);

//fclose($file_open);

//$upload -> thumb($dir_folder,$filename,$newname.$filename,$width,$height);
echo "<script language=javascript>
";
echo "parent.document.getElementById('upfile_dir_name').value='".$dir_folder.$filename."';";
echo "parent.document.getElementById('upfile_sha1').value='".$file_sha1."';";
echo "parent.document.getElementById('upfile_name').value='".$f_name."';";
echo "parent.document.getElementById('upfile_size').value='".$filesize."';";
echo "parent.document.getElementById('upfile_extname').value='".$fileextname."';";
echo "parent.document.getElementById('upfile_info_13').value='".$fw["dev"]."';";
echo "parent.document.getElementById('upfile_info_14').value='".$fw["ino"]."';";
echo "parent.document.getElementById('upfile_info_15').value='".$fw["mode"]."';";
echo "parent.document.getElementById('upfile_info_16').value='".$fw["nlink"]."';";
echo "parent.document.getElementById('upfile_info_17').value='".$fw["uid"]."';";
echo "parent.document.getElementById('upfile_info_18').value='".$fw["gid"]."';";
echo "parent.document.getElementById('upfile_info_19').value='".$fw["rdev"]."';";
echo "parent.document.getElementById('upfile_info_20').value='".$fw["size"]."';";
echo "parent.document.getElementById('upfile_info_21').value='".date("Y-m-d H:i:s",$fw["atime"])."';";
echo "parent.document.getElementById('upfile_info_22').value='".date("Y-m-d H:i:s",$fw["mtime"])."';";
echo "parent.document.getElementById('upfile_info_23').value='".date("Y-m-d H:i:s",$fw["ctime"])."';";
echo "parent.document.getElementById('upfile_info_24').value='".$fw["blksize"]."';";
echo "parent.document.getElementById('upfile_info_25').value='".$fw["blocks"]."';";

echo "parent.document.getElementById('upfile_info_box').style.display='block';";


echo "</script>";
//$image_size=getimagesize($dir_folder.$filename);
//$content="<img src=\"".$dir_folder.$filename."\"></br></br>注意---上传图片的宽度:".$image_size[0]."px 高度:".$image_size[1]."px 文件大小:".$filesize;
//echo "<script language=javascript> parent.document.getElementById('image').innerHTML = '".$content."';<//script>";
//echo "<script language=javascript> parent.document.getElementById('img2').value = '".$dir_folder.$newname.$filename."';<//script>";
//echo "<script language=javascript> parent.document.getElementById('image').innerHTML='".$content."';<//script>";
	exit;
}elseif($_GET['action']=="up_wrong"){
	alert("出错了！错误代码：up_010。请联系林聪。");
	}
		
}

?>
